//
//  Log.swift
//  ESVUtil
//
//  Created by Vencent on 2020/11/12.
//

import Foundation
import SwifterSwift
import XCGLogger

/// Log 文件路径
var logPath: String = {
    let cachePaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory,
                                                         FileManager.SearchPathDomainMask.userDomainMask, true)
    let cachePath = cachePaths.first ?? NSHomeDirectory() + "/Library/Caches"
    let fileName = Date().string(withFormat: "yyyy-MM-dd") + ".log"
    
    let key = Config.identifier + ".log.folder"
    let lastPath = String.init(data: keychain[data: key] ?? Data(), encoding: .utf8) ?? ""
    print(lastPath)
    let path: String
    if lastPath.count > 0 {
        path = lastPath
    }else {
        path = String.random(ofLength: 16)
        keychain[data: key] = path.data(using: .utf8)
    }
    let folderPath = cachePath + "/" + path
    let filePath = folderPath + "/" + fileName
    
    let manager = FileManager.default
    var isDir: ObjCBool = true
    if !manager.fileExists(atPath: folderPath, isDirectory: &isDir) {
        try? manager.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil)
    }
    if !manager.fileExists(atPath: filePath) {
        manager.createFile(atPath: filePath, contents: nil, attributes: nil)
    }
    
    return filePath
}()

///  Log 实例
public let log: XCGLogger = {
    let identifier = Config.identifier + ".logger"
    let log = XCGLogger(identifier: identifier, includeDefaultDestinations: false)
    
    // Customize as needed
    // Create a destination for the system console log (via NSLog)
    let systemDestination = AppleSystemLogDestination(identifier: identifier + ".system")

    // Optionally set some configuration options
    systemDestination.outputLevel = .debug
    systemDestination.showLogIdentifier = false
    systemDestination.showFunctionName = true
    systemDestination.showThreadName = true
    systemDestination.showLevel = true
    systemDestination.showFileName = true
    systemDestination.showLineNumber = true
    systemDestination.showDate = true

    // Add the destination to the logger
    log.add(destination: systemDestination)

    // Create a file log destination
    
    let fileDestination = FileDestination(writeToFile: logPath, identifier: identifier + ".file", shouldAppend: true)
    
    // Optionally set some configuration options
    fileDestination.outputLevel = .debug
    fileDestination.showLogIdentifier = false
    fileDestination.showFunctionName = true
    fileDestination.showThreadName = true
    fileDestination.showLevel = true
    fileDestination.showFileName = true
    fileDestination.showLineNumber = true
    fileDestination.showDate = true

    // Process this destination in the background
    fileDestination.logQueue = XCGLogger.logQueue

    // Add the destination to the logger
    log.add(destination: fileDestination)

    // Add basic app info, version info etc, to the start of the logs
    log.logAppDetails()
    
    return log
}()
